<template>
    <div class="container">
        <div class="title">
            <span>信息设置</span>
        </div>
        <el-divider></el-divider>
        <br/>
        <el-form label-width="100px"
                 :model="form"
                 status-icon
                 ref="form"
                 @submit.native.prevent>
            <el-form-item label="地址" prop="address">
                <el-input size="medium"
                          v-model="form.address"
                          placeholder="请填写地址"></el-input>
            </el-form-item>
            <el-form-item label="联系方式" prop="phone">
                <el-input size="medium"
                          v-model="form.phone"
                          placeholder="请填写联系方式"></el-input>
            </el-form-item>
            <el-form-item label="地址坐标" prop="coordinate">
                <el-input size="medium" v-model="form.coordinate" placeholder="请填写地址坐标"></el-input>
                <a href="https://lbs.qq.com/tool/getpoint/" target="_blank">腾讯坐标拾取网址</a>
            </el-form-item>
            <el-form-item label="交通枢纽" prop="traffic">
                <el-button type="primary" plain @click="handleAdd" icon="el-icon-plus">添加选项</el-button>
            </el-form-item>
            <el-form-item label="选项" v-for="(item,index) in form.traffic" prop="name" v-bind:key="index">
                <el-select  size="medium" filterable v-model="item.traffic_id" placeholder="请选择" clearable style="width: 20%;">
                    <el-option
                            v-for="(items, index) in traffic"
                            :key="index"
                            :label="items.name"
                            :value="items.id">
                    </el-option>
                </el-select>
                <el-input size="medium" v-model="item.name" placeholder="请填写名称" style="width: 20%;margin-left: 10px;"></el-input>
                <el-input size="medium" v-model="item.explain" placeholder="请填写说明" style="width: 20%;margin-left: 10px;"></el-input>
                <el-button type="danger" style="margin-left: 20px;" @click="delAnswer(index)">删除</el-button>
            </el-form-item>
            <el-form-item class="submit">
                <el-button type="primary" @click="submitForm('form')">保 存</el-button>
                <el-button @click="resetForm('form')">重 置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
import admin from '@/models/admin'
import traffic from '@/models/traffic'
import User from '@/lin/models/user'

export default {
  data() {
    return {
      traffic: [],
      form: {
        id: '',
        lin_user_id: '',
        address: '',
        coordinate: '',
        traffic: [],
        phone: '',
      },
    }
  },
  mounted() {
    this.getAdminUser()
  },
  methods: {
    delAnswer(index) {
      this.form.traffic.splice(index, 1)
    },
    async getAdminUser() {
      const res = await User.getInformation()
      this.form.lin_user_id = res.linUserId
      this.getSystem()
    },
    // 获取详情
    async getSystem() {
      const adminInfo = await admin.getAdminInfo(this.form.lin_user_id)
      if (adminInfo) {
        this.form = {
          id: adminInfo.id,
          address: adminInfo.address,
          coordinate: adminInfo.coordinate,
          lin_user_id: this.form.lin_user_id,
          traffic: adminInfo.traffic,
          phone: adminInfo.phone,
        }
      }
      this.getTrafficAll()
    },
    // 添加交通枢纽
    handleAdd() {
      this.form.traffic.push({
        traffic_id: '',
        name: '',
        explain: '',
      })
    },
    // 获取全部交通枢纽
    async getTrafficAll() {
      this.traffic = await traffic.getTrafficAll()
    },
    // eslint-disable-next-line no-unused-vars
    async submitForm(formName) {
      const self = this
      // eslint-disable-next-line no-unused-vars
      const info = {
        id: self.form.id,
        address: self.form.address,
        coordinate: self.form.coordinate,
        lin_user_id: self.form.lin_user_id,
        traffic: self.form.traffic,
        phone: self.form.phone,
      }
      try {
        // eslint-disable-next-line no-unused-vars
        const res = await admin.createAdmin(info)
        if (res.error_code === 0) {
          this.$message.success(`${res.msg}`)
        }
      } catch (error) {
        console.log(error)
      }
    },
    // 重置表单
    resetForm() {
      this.getSystem()
    },
    change(val) {
      this.form.contact_us = val
    },
  },
}
</script>

<style lang="scss" scoped>
    .el-divider--horizontal {
        margin: 0
    }

    .container {
        .title {
            height: 59px;
            line-height: 59px;
            color: $parent-title-color;
            font-size: 16px;
            font-weight: 500;
            text-indent: 40px;

            .back {
                float: right;
                margin-right: 40px;
                cursor: pointer;
            }
        }

        .wrap {
            padding: 20px;
        }

        .submit {
            float: left;
        }
    }
</style>
